package cn.edu.tju.wiki.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Constant {
	public static final String [] keySet = {"sd_era", "sd_name", "sd_authority", "sd_discover_site", "sd_textual", "sd_level"};
	public static final String[] txtKeySet = { "sd_no", "sd_name", "sd_era", "sd_discover_time",
			"sd_discover_site", "sd_authority"};
	public static final boolean [] isVisable = {true, true, false, false, false, false, false};
	public static final String [] matchType = {"equal", "contain", "equal", "equal", "equal","euqal", "equal"};
	public static final String [] keywords0 = {"朝代", "器型", "收藏", "出土遗址", "纹饰", "出土时间", "其他"};
	public static final String [][] keywords1 = {{"商代", "西周", "春秋", "戰國"}, {"食器", "酒器", "水器", "乐器", "兵器"}, {"中国", "国外"}, {}, {}, {}, {}};
	public static final String [][][] keywords2 = {
		{
			{"商代中期", "商代晚期", "商代晚期或西周早期"},
			{"西周", "西周早期", "西周早期或中期", "西周中期", "西周中期或晚期","西周晚期", "西周晚期或春秋早期"},
			{"春秋", "春秋早期", "春秋中期", "春秋中期或晚期", "春秋晚期", "春秋戰國", "春秋晚期或戰國早期"},
			{"戰國", "戰國早期", "戰國早期或中期", "戰國中期", "戰國中期或晚期", "戰國晚期"}},
		{
			{"鼎", "鬲", "甗", "簋", "簠", "盨", "敦", "豆"},
			{"爵", "角", "斝", "觚", "觶", "兕觥", "尊", "卣", "盉", "方彝", "勺"},
			{"罍", "壺", "盤", "匜", "瓿", "盂", "盉", "鑑"},
			{"鐃", "鐘", "鎛", "鼓座", "錞", "鉦鋮", "鐃", "鐸", "鎛鐘", "甬鐘", "钮钟"},
			{"鉞", "戈", "矛", "戟", "劍", "鈹", "鏃", "盾钖"}},
		{
			{"散居各地", "考古研究所", "首都博物館", "寶雞青銅器博物院", "上海博物館",
				"旅順博物館", "台北故宮博物院", "中國國家博物館", "中國歷史博物館",
				"中國社會科學院考古研究所", "內蒙古", "北京", "南京", "台北", "吉林",
				"四川", "天津", "安徽", "安陽", "山東", "山西", "岐山縣博物館", "廣州",
				"廣東", "廣西", "曲阜市文物管理局", "武功縣文化館", "武漢", "江蘇", "江西",
				"河北", "河南", "洛陽博物館", "浙江", "清華大學圖書館", "湖北", "湖南",
				"滕州博物館", "澳門", "甘肅", "遼寧", "陝西", "香港"},
			{"美國", "丹麥", "倫敦", "加拿大", "德國", "新加坡", "比利時", "日本",
					"法國", "澳大利亞", "瑞典", "瑞士", "義大利", "英國", "荷蘭"}},
		{},
		{},
		{},
		{}};
	public static List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
	public static List<Map<String, Object>> documentList = new ArrayList<Map<String, Object>>();
	public static Map<String,NodeItem> leafNodes = new HashMap<String, NodeItem>();
	public static Map<String,NodeItem> parentNodes = new HashMap<String, NodeItem>();
	public static Map<String,NodeItem> ancestorNodes = new HashMap<String, NodeItem>();
	public static NodeItem rootOriNode;
	public static List<Map<String, Object>> relicsOriInfo;
	public static List<NodeItem> oriNodeLst = new ArrayList<NodeItem>();
	
	public static String matrixFilePath = "/Users/liudajian/Documents/workshop/tomcat/webapps/CH/d3-pages/wikidata/data/matrix.js";
	public static String heritageTxtPath = "/Users/liudajian/Workspaces/MyEclipse 10/CH/WebRoot/d3-pages/wikidata/data/heritage.txt";
	public static String wordCloudJsonPath = "/Users/liudajian/Documents/workshop/tomcat/webapps/CH/d3-pages/wikidata/data/wordcloud.json";
	static{
		try {
			readData();
			readDocument();
			initInfoNodes();
//			readDataFromTxt();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void readDocument() throws Exception {
		String qryStr = "select * from document_data";
		String schema = "my_wikidb";
		String[] keySet = { "doc_id", "doc_book", "doc_content"};
		documentList = DataCon.getDataList(schema, qryStr, keySet);
	}
	
	public static void readData() throws Exception {
		String qryStr = "select * from sinica_data";
		String schema = "my_wikidb";
		String[] keySet = { "sd_no", "sd_name", "sd_era", "sd_discover_time",
				"sd_discover_site", "sd_discover_adrr", "sd_authority",
				"sd_transfer", "sd_size", "sd_textual", "sd_imgUrl",
				"sd_content_explain", "sd_content_imgUrl", "sd_id" };
		dataList = DataCon.getDataList(schema, qryStr, keySet);
		for (int i = 0; i < dataList.size(); i++) {
			for (int j = 0; j < keySet.length; j++) {
				dataList.get(i).put(
						keySet[j],
						dataList.get(i).get(keySet[j]).toString()
								.replaceAll("&nbsp;", "").replaceAll("\\", "")
								.replaceAll("\\", "").replaceAll("\n", "")
								.replaceAll("                ", "").replaceAll("            ", ""));
			}
			Map<String, Object> itemMap = dataList.get(i);
			itemMap.put("sd_discover_site", 
					itemMap.get("sd_discover_site").toString().contains("<") ? 
					itemMap.get("sd_discover_site").toString()
					.substring(0,itemMap.get("sd_discover_site").toString().indexOf("<"))
					: itemMap.get("sd_discover_site").toString());
			itemMap.put("sd_authority",
					itemMap.get("sd_authority").toString().contains("<") ? 
					itemMap.get("sd_authority").toString()
					.substring(0,itemMap.get("sd_authority").toString().indexOf("<"))
					: itemMap.get("sd_authority").toString());
			dataList.set(i, itemMap);
		}
	}
	
	private static void initInfoNodes() {
		// TODO Auto-generated method stub
		for (int i = 0; i < keywords0.length; i++) {
			NodeItem newAncNode = new NodeItem(keywords0[i]);
			if(isVisable[i]){
				for (int j = 0; j < keywords1[i].length; j++) {
					NodeItem newParNode = new NodeItem(keywords1[i][j]);
					for (int k = 0; k < keywords2[i][j].length; k++) {
						NodeItem newLeaNode = new NodeItem(keywords2[i][j][k]);
						leafNodes.put(keywords2[i][j][k], newLeaNode);
						newParNode.childrenList.add(newLeaNode);
					}
					parentNodes.put(keywords1[i][j], newParNode);
					newAncNode.childrenList.add(newParNode);
				}
			}
			ancestorNodes.put(keywords0[i], newAncNode);
		}
	}

	public static void readDataFromTxt(){
		try {
			String encoding = "UTF8";
			File file = new File(heritageTxtPath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				String [] splitedTxt = null;
				
				while ((lineTxt = bufferedReader.readLine()) != null) {
					System.out.println(lineTxt);
					Map<String, Object> newItem = new HashMap<String, Object>();
					splitedTxt = lineTxt.split(",");
					for (int i = 0; i < txtKeySet.length; i++) {
						newItem.put(txtKeySet[i], splitedTxt[i]);
					}
					dataList.add(newItem);
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
	}
}
